import { ICredentials } from '@serverless-devs/component-interface';
import Sls20201230, { InitSlsRequest } from '@serverless-cd/srm-aliyun-sls20201230';
import { Config } from '@alicloud/openapi-client';
import { IRegion } from '../../interface';
import { PROJECT, LOG_STORE } from '../../default/resources';
import logger from '../../logger';

export default class Sls {
  static generateProjectName = (region: IRegion, accountID: string): string =>
    PROJECT || `${accountID}-${region}-project`;
  static generateLogstoreName = (): string => {
    // 3.0 仅创建一个 logstore
    return LOG_STORE;
    // // -20 是因为要抛去生成名称的前缀（fc-service-）和后缀（-logstore）
    // if (name.length > 60 - 20) {
    //   const prefix = name.slice(0, 6).toLocaleLowerCase();
    //   const md5Uid = crypto.createHmac('md5', name).update(name).digest('hex');
    //   return `${prefix}-${md5Uid.slice(0, 7)}`;
    // }

    // return `fc-v3-${name}-logstore`.toLocaleLowerCase();
  };

  readonly client: Sls20201230;
  private accountID: string;

  constructor(
    private region: IRegion,
    credentials: ICredentials,
  ) {
    const config = new Config({
      accessKeyId: credentials.AccessKeyID,
      accessKeySecret: credentials.AccessKeySecret,
      securityToken: credentials.SecurityToken,
      endpoint: `${region}.log.aliyuncs.com`,
      userAgent: 'serverless-devs',
    });
    this.client = new Sls20201230(config);
    this.accountID = credentials.AccountID;
  }

  async deploy(): Promise<{ project: string; logstore: string }> {
    const project = Sls.generateProjectName(this.region, this.accountID);
    const logstore = Sls.generateLogstoreName();

    const request = new InitSlsRequest({
      project,
      logstore,
      description: 'Generated by Aliyun fc3.x component',
    });

    logger.spin('creating', 'sls', `project: ${project}; logstore: ${logstore}`);
    await this.client.initSls(request);
    logger.spin('created', 'sls', `project: ${project}; logstore: ${logstore}`);
    return { project, logstore };
  }
}
